package com.hs.shirodemo2.controller;

import com.hs.shirodemo2.constant.CommonConstant;
import com.hs.shirodemo2.constant.SystemCode;
import com.hs.shirodemo2.entity.SysUser;
import com.hs.shirodemo2.service.FileUploadService;
import com.hs.shirodemo2.service.MenuService;
import com.hs.shirodemo2.service.SysUserService;
import com.hs.shirodemo2.utils.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private FileUploadService fileUploadService;
    @Autowired
    private MenuService menuService;

    /**
     * 新增
     */
    @RequiresPermissions("user:add")
    @PostMapping("/add")
    public Result add(@RequestBody SysUser sysUser) {
        sysUserService.add(sysUser);
        return Result.success();
    }

    /**
     * 编辑
     */
    //@RequiresPermissions("user:edit")
    @PutMapping("/edit")
    public Result edit(@RequestBody SysUser sysUser) {
        sysUserService.edit(sysUser);
        return Result.success();
    }

    /**
     * 删除
     */
    @RequiresPermissions("user:remove")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Integer id) {
        sysUserService.remove(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @RequiresPermissions("user:remove")
    @DeleteMapping("/remove")
    public Result removeBatch(Integer[] ids) {
        sysUserService.removeBatch(ids);
        return Result.success();
    }

    /**
     * 单条查询
     */
    @RequiresPermissions("user:query")
    @GetMapping("/find/{id}")
    public Result findById(@PathVariable Integer id) {
        return Result.success(sysUserService.findById(id));
    }

    /**
     * 分页查询
     */
    @RequiresPermissions("user:query")
    @RequestMapping("/find/{pageNum}/{pageSize}")
    public Result findPage(
            @PathVariable Integer pageNum,
            @PathVariable  Integer pageSize,
            @RequestBody  SysUser sysUser) {
        return Result.success(sysUserService.findPage(pageNum, pageSize, sysUser));
    }

    /**
     * 判断用户名是否重复
     */
    @RequiresPermissions("user:query")
    @GetMapping("/findExists/{username}")
    public Result findUsernameExists(@PathVariable String username) {
        SysUser sysUser = sysUserService.findByUsername(username);
        if (sysUser == null) {
            return Result.success();
        }
        return Result.error(SystemCode.USERNAME_EXISTS.getCode(), SystemCode.USERNAME_EXISTS.getMessage(), null);
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        String path = fileUploadService.uploadOneFile(file);
        path = path.replace("\\","/");
        return Result.success(path);
    }

    @GetMapping("/getData")
    public Object getData() {
        /*[
			{ value: 1048, name: 'Search Engine' },
			{ value: 735, name: 'Direct' },
			{ value: 580, name: 'Email' },
			{ value: 484, name: 'Union Ads' },
			{ value: 300, name: 'Video Ads' }
		]*/
        List list = new ArrayList();
        for(int i=1;i<=10;i++){
            Map map = new HashMap();
            map.put("name","李四"+i);
            map.put("value",new Random().nextInt(30)+i);
            list.add(map);
        }
        return Result.success(list);
    }


}